import 'package:flutter/material.dart';

import '../package_model_comm.dart';

class MultiNormalSelectChip extends StatefulWidget {
  ///true单选 false多选
//  final bool isSingle;

  /// 标签全部的list
//  final List<String> dataList;
  List<BaseModel> dataList = [];

  /// 标签选择的list
  final List<String> selectList;

  ///选择回调事件
  final Function(List<String>) onSelectionChanged;

  MultiNormalSelectChip(this.dataList, {this.selectList, this.onSelectionChanged});

  @override
  _MultiNormalSelectChipState createState() => _MultiNormalSelectChipState();
}

class _MultiNormalSelectChipState extends State<MultiNormalSelectChip> {


//  List selectTitles = List();


  _buildChoiceList() {
    List<Widget> choices = List();
    widget.dataList.forEach((title) {
      choices.add(Container(
        height: 35,
        margin: EdgeInsets.all(4),
        child: ChoiceChip(
          label: Text(
            title.name,
            style: TextStyle(fontSize: 14, color: widget.selectList.contains(title.name)
                ?Colors.white
                :Colors.grey),
          ),
          selected: widget.selectList.contains(title.name),
          materialTapTargetSize: MaterialTapTargetSize.padded,
          padding: EdgeInsets.only(left: 8, right: 8),
          selectedColor: Colors.blue,
          backgroundColor: Color(0xFFF5F5F5),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(4),
          ),
          onSelected: (selected) {
            print(selected);
            if(selected){
              setState(() {
                widget.selectList.add(title.name);
              });

            }else {
              setState(() {
                widget.selectList.remove(title.name);
              });

            }

            widget.onSelectionChanged(widget.selectList);
          },
        ),
      ));
    });

    return choices;
  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
//    selectTitles.addAll(widget.selectList);

  }

  @override
  Widget build(BuildContext context) {
    return Wrap(
      alignment: WrapAlignment.start,///start左至右排列 end右至左排列
      children: _buildChoiceList(),
    );
  }
}